home *** CD-ROM | disk | FTP | other *** search
/ DOpus Plus / DOpus Plus.iso / Tutorial / C Guide / Time_module / ModuleEntry.c < prev    next >
C/C++ Source or Header  |  1998-09-20  |  9KB  |  265 lines

  1.  
  2. /* ModuleEntry.c
  3.  
  4.    Of course you may do all your work here, but it is nicer for
  5.    overview, you do here only some simple stuff
  6.     
  7. */
  8.  
  9. #include "includes/Project.h"
  10.  
  11.  
  12. /* At first here you *must* declare your ModuleInfo structure */
  13.  
  14. ModuleInfo module_info =
  15. {
  16.     2,                      // version - your choice...
  17.     "time.module",          // module name  (case sensitive !)
  18.     "time.catalog",         // catalog name (case sensitive !)
  19.     MODULEF_CALL_STARTUP,   // flags
  20.     1,                      // number of functions
  21.     { 0, COMMAND_0, MSG_CLOCK_DESC, NULL, CMD_0_TEMPL }
  22. };
  23.  
  24. /* If you want to add more functions, set the number before and
  25.    declare ie. :
  26.    
  27. ModuleFunction module_func[1] =
  28.     { 1, COMMAND_1, NULL, FUNCF_PRIVATE, NULL }   
  29. };
  30.  
  31. */
  32.  
  33.  
  34. /* If you are a really nice one, you may add at the following string
  35.    a \0 before and after the string... (I am sooo lazy ;-) ) */
  36.    
  37. static char version[] = "$VER: " VERSION_STRING " " __AMIGADATE__;
  38.  
  39. /********************************************************************/
  40. // EXTERNALS
  41.  
  42. extern void Deteach( struct Screen *screen );
  43.  
  44. /********************************************************************/
  45. // GLOBALS AND LOCAL FUNCTIONS
  46.  
  47. IPCData *clock_ipc;
  48.  
  49. ULONG ScanSub( STRPTR template, STRPTR args, USHORT template_count );
  50. void PrepareClock( struct Screen *screen, char *args );
  51.  
  52. /********************************************************************/
  53. // OUR ENTRY
  54.  
  55. int __asm __saveds L_Module_Entry( register __a0 char *args,              
  56.                                    register __a1 struct Screen *screen,   
  57.                                    register __a2 IPCData *ipc,
  58.                                    register __a3 IPCData *main_ipc,
  59.                                    register __d0 ULONG mod_id,
  60.                                    register __d1 EXT_FUNC(func_callback) )
  61. {       
  62.       char buffer[4];
  63.          
  64.       switch( mod_id )
  65.         {
  66.            case FUNCID_STARTUP:
  67.                                  /* Check - Should we start ? - If yes, there must be */
  68.                                  /* our variable in ENV:dopus and must contain "1".   */
  69.                                  /* If not, we simply return at this stage. */
  70.                                  
  71.                                  if( GetVar(CLOCKSTARTVAR, buffer, 4, LV_VAR|GVF_GLOBAL_ONLY) < 1 ||
  72.                                       buffer[0] != '1' )
  73.                                  return 1;
  74.                                  
  75.                                  /* Give DOpus some time to go on, */
  76.                                  /* so we must later not wait...   */
  77.                                   
  78.                                  Delay( 300 );
  79.                        
  80.            case 0:               
  81.                                  /* we do the same in every case */
  82.                                  PrepareClock( screen, args );                                 
  83.                                  break;            
  84.         }
  85.       
  86.       /* Always modules should return 1 ... */   
  87.       return 1;
  88. }
  89.  
  90. /********************************************************************/
  91. /* This function does parse the arguments (, if there are some) and */
  92. /* launches the clock deteaching routine, if the clock is not       */
  93. /* already running. If the clock does run, it does pass the given   */
  94. /* arguments to it or even a zero command. So is later no different */
  95. /* code needed to take care of the arguments.                       */
  96. /********************************************************************/  
  97.  
  98. void PrepareClock( struct Screen *screen, char *args )
  99. {
  100.    ULONG command   = NULL, ctr;
  101.    FuncArgs *fargs = NULL;
  102.    CPrefsData *cp  = NULL;
  103.  
  104. /* Like you here see, you should always check if there are arguments  */
  105. /* before you try to parse them. ParseArgs() does not accept NULL !   */
  106.       
  107.    if( *args &&  (fargs = ParseArgs(CMD_0_TEMPL, args)) )
  108.      {
  109.         /* I use here AllocVec(), because I use later the data_free   */
  110.         /* element of IPC_Command()                                   */
  111.         
  112.         if( (cp = AllocVec(sizeof(CPrefsData), MEMF_CLEAR)) )
  113.           {
  114.              /* At first we make a note, what arguments are given     */
  115.              
  116.              for( ctr = 0; ctr <= CLOCKARG_COUNT; ctr++ )
  117.                   if( fargs->FA_Arguments[ctr] )
  118.                        command |= 1 << ctr;
  119.              
  120.              /* If a delay is given, we do it now and delete the bit  */
  121.              /* in command                                            */
  122.              
  123.              if( ARG_DELAY )
  124.                {
  125.                   Delay( *(ULONG *) ARG_DELAY );
  126.                   command ^= 1 << 2;
  127.                }
  128.              
  129.              /* Now we are ready to store all values step by step     */
  130.                           
  131.              if( ARG_FONT )
  132.                   strcpy( cp->fontname, (STRPTR) ARG_FONT );
  133.         
  134.              if( ARG_SIZE )
  135.                   cp->txtattr.ta_YSize = *(ULONG *) ARG_SIZE;
  136.                     
  137.              if( ARG_APEN )
  138.                   cp->ibox.Width       = *(ULONG *) ARG_APEN;
  139.                     
  140.              if( ARG_BPEN )
  141.                   cp->ibox.Height      = *(ULONG *) ARG_BPEN;
  142.                     
  143.              if( ARG_MODE )
  144.                {
  145.                   /* Mode has a subtemplate and must be parsed again */
  146.                   
  147.                   cp->txtattr.ta_Flags = ScanSub( MODE_TEMPL, (STRPTR) ARG_MODE, 4 );
  148.                                     
  149.                   if( cp->txtattr.ta_Flags & 1 << 0 )
  150.                        cp->txtattr.ta_Flags = 0;
  151.                   else
  152.                        cp->txtattr.ta_Flags >>= 1;
  153.                }
  154.                  
  155.              if( ARG_STYLE )
  156.                {
  157.                   cp->txtattr.ta_Style = ScanSub( STYLE_TEMPL, (STRPTR) ARG_STYLE, 4 );
  158.                   
  159.                   if( cp->txtattr.ta_Style & 1 << 0 )
  160.                        cp->txtattr.ta_Style = 0;
  161.                   else
  162.                        cp->txtattr.ta_Style >>= 1;          
  163.                }
  164.           
  165.              if( ARG_BORDER )
  166.                {
  167.                   if( ScanSub( ON_OFF_TEMPL, (STRPTR) ARG_BORDER, 8 ) < NO_CASE )
  168.                       command |= BORDER_ON;
  169.                   else
  170.                       command |= BORDER_OFF;                        
  171.                }
  172.             
  173.              if( ARG_PIC )
  174.                   strcpy( cp->picture, (STRPTR) ARG_PIC );
  175.                                                   
  176.              if( ARG_LEFT )
  177.                   cp->ibox.Left = *(ULONG *) ARG_LEFT;
  178.                               
  179.              if( ARG_TOP )
  180.                   cp->ibox.Top  = *(ULONG *) ARG_TOP;
  181.         
  182.              if( ARG_ALARM )
  183.                {
  184.                   /* If ScanSub() does return 0, we should have a time */
  185.                   /* as argument ( = else )                            */
  186.                   
  187.                   if( (ctr = ScanSub(ON_OFF_TEMPL, (STRPTR) ARG_ALARM, 8)) )
  188.                     {
  189.                        if( ctr < NO_CASE )
  190.                             command |= ALARM_ON;
  191.                        else
  192.                             command |= ALARM_OFF;
  193.                     }
  194.                   else
  195.                     {
  196.                        strcpy( cp->alarm, (STRPTR) ARG_ALARM );
  197.                        command |= ALARM_ON;
  198.                     }
  199.                }
  200.          
  201.              if( ARG_SOUND )
  202.                {
  203.                   if( ScanSub(ON_OFF_TEMPL, (STRPTR) ARG_SOUND, 8) < NO_CASE )
  204.                        command |= SOUND_ON;
  205.                   else
  206.                        command |= SOUND_OFF;
  207.                }
  208.           }
  209.      }
  210.     
  211.    if( !clock_ipc )
  212.      {
  213.         Deteach( screen );
  214.         while( !clock_ipc )
  215.                 Delay( 20 );
  216.      }
  217.      
  218.    IPC_Command( clock_ipc, command, NULL, NULL, cp, REPLY_NO_PORT );                                 
  219.         
  220.    if( fargs )
  221.         DisposeArgs( fargs );
  222.          
  223. }
  224.  
  225. /********************************************************************/
  226.  
  227. ULONG ScanSub( STRPTR template, STRPTR args, USHORT template_count )
  228. {
  229.       UBYTE     ctr;
  230.       ULONG     ret = NULL;
  231.       STRPTR    ptr;
  232.       FuncArgs *fargs;
  233.       
  234.       ptr = args;
  235.       
  236.       /* More than one argument should be added by the user with     */
  237.       /* a "|", but for parsing we need spaces. So we simply replace */
  238.       /* the "|" by spaces...                                        */ 
  239.                                 
  240.       while( *ptr )
  241.         {
  242.            if( *ptr == '|' )
  243.                *ptr =  ' ';
  244.            ptr++;
  245.         }
  246.         
  247.       /* Now we have a "normal" argument string and can parse it      */ 
  248.                     
  249.       if( (fargs = ParseArgs(template, args)) )
  250.         {
  251.            for( ctr = 0; ctr < template_count; ctr++ )
  252.                 if( fargs->FA_Arguments[ctr] )
  253.                      ret |= 1 << ctr;
  254.            
  255.            DisposeArgs( fargs );                     
  256.         }   
  257.       
  258.       return ret; 
  259. }
  260.  
  261. /********************************************************************/
  262.  
  263.  
  264.